package com.share.dgs.fetcher;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.netflix.graphql.dgs.DgsComponent;
import com.netflix.graphql.dgs.DgsData;
import com.netflix.graphql.dgs.InputArgument;
import com.share.dgs.mapper.AddressMapper;
import com.share.dgs.model.Address;
import graphql.relay.Connection;
import graphql.relay.SimpleListConnection;
import graphql.schema.DataFetchingEnvironment;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Objects;

/**
 * @author caifan
 * @created 2021/3/5
 * description:
 */
@DgsComponent
public class AddressFetcher {

    @Autowired
    private AddressMapper addressMapper;

    @DgsData(parentType = "Query", field = "getAddress")
    public Address getAddress(@InputArgument("id")Integer id) {
        return addressMapper.selectById(id);
    }

    @DgsData(parentType = "Query", field = "listAddress")
    public List<Address> listAddress() {
        return addressMapper.selectList(Wrappers.emptyWrapper());
    }

    /**
     * 分页查询
     * @param environment
     * @param customerId 查询条件
     * @param first 后面的的多少条结果
     * @param after 每条数据都有游标，当前游标后的几条数据 与first配合使用，null则从第一条开始
     * @param last 前面多少条数据
     * @param before  当前游标前的几条数据 与last配合使用，null则从最后一条开始
     * @return
     */
    @DgsData(parentType = "Query", field = "pageAddress")
    public Connection<Address> pageAddress(DataFetchingEnvironment environment, @InputArgument("customerId")Integer customerId, @InputArgument("first")Integer first,
                                           @InputArgument("after")String after, @InputArgument("last")Integer last, @InputArgument("before")String before) {
        List<Address> addressList = addressMapper.selectList(Wrappers.<Address>lambdaQuery().eq(Address::getCustomerId, customerId));
        return new SimpleListConnection(addressList).get(environment);
    }

    @DgsData(parentType = "Mutation", field = "addAddress")
    public int addAddress(@InputArgument("input")AddressInput addressInput) {
        if (Objects.isNull(addressInput)) {
            return 0;
        }
        Address address = new Address();
        BeanUtils.copyProperties(addressInput, address);
        return addressMapper.insert(address);
    }

    @DgsData(parentType = "Mutation", field = "updateAddress")
    public Address updateAddress(@InputArgument("id")Integer id, @InputArgument("street")String street, @InputArgument("city")String city) {
        if (Objects.isNull(id) || Objects.isNull(street)) {
            return null;
        }
        Address address = new Address();
        address.setStreet(street);
        addressMapper.update(address, Wrappers.<Address>lambdaUpdate().eq(Address::getId, id).set(Address::getCity, city));
        return addressMapper.selectById(id);
    }

}

@Data
class AddressInput {
    private String street;
    private String city;
    private String country;
    private Integer customerId;
}

